import {Component, OnInit} from '@angular/core';
import {AttrMatchService} from './attr-match-service';

@Component({
  selector: 'app-add-match',
  templateUrl: 'attr-match.component.html',
  styleUrls: ['attr-match.component.scss'],
  providers: [AttrMatchService]
})
export class AttrMatchComponent implements OnInit {

  loadingShow = false;
  attributeListShow = false;
  type: string;
  message: string;
  isShow = false;

  oneCategoryList = []; // 一级类目
  nowOneCategory;

  twoCategoryList = []; // 二级类目
  nowTwoCategory;

  threeCategoryList = []; // 三级类目
  nowThreeCategory;

  goodsAttribute = []; // 一级类目的属性集名
  nowGoodsAttribute; // 当前选中的属性名

  goodsAttributeValue = [];
  nowGoodsAttributeValue; // 当前选中的属性值

  goodsAttributeSetList = [];
  goodsAttributeSetValueList = [];


  constructor(private attrMatchService: AttrMatchService) {
  }

  ngOnInit() {
    this.getCategoryList(0, 0);
  }

  getCategoryList(level, parent) {
    this.attrMatchService.getCategoryList(level, parent).subscribe(
      res => {
        switch (level) {
          case 0:
            if (res.data.data) {
              this.oneCategoryList = res.data.data;
            }
            break;
          case 1:
            if (res.data.data) {
              this.twoCategoryList = res.data.data;
            }
            break;
          case 2:
            if (res.data.data) {
              this.threeCategoryList = res.data.data;
            }
            break;
        }
      }
    );
  }

  selectNowCategory(item) {
    this.goodsAttribute = []; // 一级类目的属性集名
    this.nowGoodsAttribute = null;
    this.goodsAttributeValue = [];
    this.nowGoodsAttributeValue = null;

    this.goodsAttributeSetList = [];
    this.goodsAttributeSetValueList = [];

    switch (item.level) {
      case 0:
        // 二级初始化
        this.twoCategoryList = []; // 二级类目
        this.nowTwoCategory = null;
        // 三级初始化
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;

        this.nowOneCategory = item;
        // 一级类目的属性集名
        // this.getGoodsAttributeById(item.id);
        this.change_view_status(false);
        break;
      case 1:
        // 三级初始化
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;

        this.nowTwoCategory = item;
        this.change_view_status(false);
        break;
      case 2:
        this.loadingShow = true;
        this.nowThreeCategory = item;

        if (this.nowOneCategory.goods_attribute_category[0]) {
          // 一级类目所对应的属性名
          this.getGoodsAttributeById(this.nowOneCategory.goods_attribute_category[0].id);
          this.change_view_status(true);
        } else {
          this.loadingShow = false;
        }

        break;
    }
    if (item.level < 2) {
      this.getCategoryList(item.level + 1, item.id);
    }
  }

  // 控制属性选择项列表显示状态
  change_view_status(status) {
    if (status) {
      this.attributeListShow = true;
    } else {
      this.attributeListShow = false;
    }
  }

  // 三级绑定属性集
  getGoodsAttributeset(id) {
    this.attrMatchService.getGoodsAttributeset(id).subscribe(res => {

      if (res.data.data) {
        this.goodsAttributeSetList = res.data.data;

        this.goodsAttributeSetList.every((val, idx, array) => {
          // console.log(val);
          // console.log(val.goods_attribute_id);
          this.goodsAttribute.every((val1, idx1, array1) => {
            // console.log(val1);
            if (val1.id === val.goods_attribute_id) {
              // 获取下一级绑定用
              val1.goods_attribute_set_id = val.id;
              // 三级是否绑定属性集
              val1.bind = true;
              val1.multiple_choice = val.multiple_choice;

              this.attrMatchService.getGoodsAttributeSetValue(val1.goods_attribute_set_id).subscribe(
                rs => {
                  if (rs.data.data) {
                    val1.child = rs.data.data;
                  } else {
                    val1.child = [];
                  }
                  // console.log(rs);
                }
              );

            }
            return true;
          });
          return true;
        });
      }
      // console.log(res);
    });
  }

  // 获取一级所对应的属性集名称
  getGoodsAttributeById(goods_attribute_category_id) {
    this.attrMatchService.getGoodsAttribute(goods_attribute_category_id).subscribe(res => {
      if (res.data.data) {
        this.loadingShow = false;
        this.goodsAttribute = res.data.data;
        // 选中３级再请求
        if (this.nowThreeCategory) {
          // 三级绑定属性集名称关系
          this.getGoodsAttributeset(this.nowThreeCategory.id);
        }
      }
      //  console.log(res);
    });
  }

  getGoodsAttributeValue(goods_attribute_id) {
    this.attrMatchService.getGoodsAttributeValue(goods_attribute_id).subscribe(res => {
      if (res.data.data) {
        this.goodsAttributeValue = res.data.data;
      }
      // 当前属性名所对应的绑定关系
      if (this.nowGoodsAttribute.goods_attribute_set_id != null) {
        this.getGoodsAttributeSetValue(this.nowGoodsAttribute.goods_attribute_set_id);
      }
    });
  }


  // goods_attribute_set 对应 goods_attribute_set_value
  getGoodsAttributeSetValue(goods_attribute_set_id) {
    this.attrMatchService.getGoodsAttributeSetValue(goods_attribute_set_id).subscribe(res => {
      if (res.data.data) {
        this.goodsAttributeSetValueList = res.data.data;

        this.goodsAttributeSetValueList.every((val, idx, array) => {
          this.goodsAttributeValue.every((val1, idx1, array1) => {

            if (val.goods_attribute_value_id === val1.id) {
              val1.bind = true;
              // 删除用
              val1.goods_attribute_set_value_id = val.id;
            }
            // console.log(val1);
            return true;
          });
          return true;
        });
      }
    });
  }


  // 当前选中属性集
  selectGoodsAttribute(item) {
    // console.log(item);
    // 获取属性名；
    // console.log(item.goods_attribute_set_id);

    // console.log(this.nowThreeCategory.id);
    this.attrMatchService.goodsAttributeSetFindByInfo(this.nowThreeCategory.id, item.id).subscribe(res => {
      if (res.data) {
        item.bind = true;
        item.goods_attribute_set_id = res.data.id;
        item.multiple_choice = res.data.multiple_choice;
      } else {
        delete item.bind;
        delete item.goods_attribute_set_id;
        delete item.multiple_choice;
      }
      this.nowGoodsAttribute = item;
      if (item.id) {
        this.getGoodsAttributeValue(item.id);
      }
    });

  }

  selectGoodsAttributeValue(item) {
    this.nowGoodsAttributeValue = item;
  }

  selectRadioGoodsAttributeValue(item) {
    this.nowGoodsAttributeValue = item;

    this.goodsAttributeValue.every((val1, idx1, array1) => {
      val1.bind = false;
      if (val1.id === item.id) {
        val1.bind = true;
      }
      // console.log(val1);
      return true;
    });

  }

  choice(status) {
    this.nowGoodsAttribute.multiple_choice = status;
    /*this.goodsAttributeValue.every((val1, idx1, array1) => {
     val1.bind = false;
     // console.log(val1);
     return true;
     });*/
  }

  bindGoodsAttributeSetAndSetValue() {
    this.nowGoodsAttribute.now_goods_category_id = this.nowThreeCategory.id;
    // 属性集所属的值
    this.nowGoodsAttribute.goodsAttributeValue = this.goodsAttributeValue;
    // 当前选中的属性集

    if (!this.nowGoodsAttribute.multiple_choice) {
      this.type = 'info';
      this.message = '请选择单选多选';
      this.isShow = true;
      return;
    }

    this.attrMatchService.bindGoodsAttributeSetAndSetValue(JSON.stringify(this.nowGoodsAttribute)).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '操作成功';
          this.isShow = true;
          this.updateGoodsAttributeSet();
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
          return;
        }
      }
    );
  }

  goodsAttributeSetDel() {
    this.attrMatchService.goodsAttributeSetDel(this.nowGoodsAttribute.goods_attribute_set_id).subscribe(res => {
      this.updateGoodsAttributeSet();
    });
  }

  updateGoodsAttributeSet() {
    this.goodsAttribute.every((val1, idx1, array1) => {
      if (val1.id === this.nowGoodsAttribute.id) {
        this.selectGoodsAttribute(val1);
      }
      return true;
    });
    this.getGoodsAttributeset(this.nowThreeCategory.id);
  }

  promptIsShow(event: any): void {
    this.isShow = event;
  }

  selectGoodsAttributeValueAll() {
    this.goodsAttributeValue.every((val, idx, array) => {
      val.bind = true;
      return true;
    });
  }

  selectGoodsAttributeValueAllCancel() {
    this.goodsAttributeValue.every((val, idx, array) => {
      val.bind = false;
      return true;
    });
  }

}
